﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Daco.Web.UI;
using System.IO;
using System.Data;
using System.Data.OleDb;
using Daco.Web.Store;

namespace Daco.Web.Pages.Secure
{
    public partial class ImportProduct : SitePage
    {
        public ImportProduct()
        {
            this.Load += new EventHandler(ImportProduct_Load);
        }

        void ImportProduct_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                // Luu tam file vao thu muc Uploads
                string tempPath = Server.MapPath("~/Uploads/Temp/");
                if (!Directory.Exists(tempPath))
                {
                    Directory.CreateDirectory(tempPath);
                }

                string filePath = tempPath + fuFile.FileName;
                if (File.Exists(filePath))
                    File.Delete(filePath);

                fuFile.SaveAs(filePath);

                // Mo ket' noi' den file excel nay
                string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + @";Extended Properties=""Excel 8.0;HDR=YES;""";

                OleDbConnection conn = new OleDbConnection(connectionString);

                try
                {
                    conn.Open();
                    OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Input Form$]", conn);
                    
                    DataTable tbl = new DataTable();
                    tbl.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection));

                    foreach (DataRow row in tbl.Rows)
                    {

                    }
                }
                catch (Exception ex)
                {
                    lbStatus.Text = ex.Message;
                }
            }
        }
    }
}